﻿using IXION.BLOG.COMMON.DATA;
using IXION.BLOG.DOMAIN.BLOG;
using NHibernate;
using NHibernate.Expression;
using System;
using System.Collections.Generic;
using System.Text;

namespace IXION.BLOG.DAO.BLOG
{
    public class ArticleDao
    {
        public Article FindArticleById(int id)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            cr.Add(Expression.Eq("Id", id));
            IList<Article> articles = cr.List<Article>();
            if (articles.Count > 0)
            {
                return articles[0];
            }
            return null;
        }
        /// <summary>
        /// 列文章
        /// </summary>
        /// <param name="start"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IList<Article> GetArticles(int start, int count)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            cr.AddOrder(Order.Desc("Id"));
            cr.SetFirstResult(start);
            cr.SetMaxResults(count);
            return cr.List<Article>();
        }

        public IList<Article> ListAllArticles()
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            cr.AddOrder(Order.Desc("Id"));
            return cr.List<Article>();
            //throw new NotImplementedException();
        }

        public IList<Article> SearchArticlesByPostDay(DateTime begin, DateTime end, int start, int count)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            cr.Add(Expression.Between("PostTime", begin, end));
            cr.AddOrder(Order.Desc("Id"));
            cr.SetFirstResult(start);
            cr.SetMaxResults(count);
            return cr.List<Article>();
            //throw new NotImplementedException();
        }

        public IList<Article> SearchArticlesByKey(string value, int start, int count)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            value = String.Format("%" + value + "%");
            cr.Add(Expression.Or(Expression.Like("Title", value), Expression.Like("Content", value)));
            cr.AddOrder(Order.Desc("Id"));
            cr.SetFirstResult(start);
            cr.SetMaxResults(count);
            return cr.List<Article>();
            //throw new NotImplementedException();
        }

        public IList<Article> SearchArticlesByKey(string value)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            value = String.Format("%" + value + "%");
            cr.Add(Expression.Or(Expression.Like("Title", value), Expression.Like("Content", value)));
            cr.AddOrder(Order.Desc("Id"));
            return cr.List<Article>();
            //throw new NotImplementedException();
        }

        public IList<Article> SearchArticlesByPostDay(DateTime begin, DateTime end)
        {
            ISession session = NHibernateHelper.Instance.GetSession();
            ICriteria cr = session.CreateCriteria(typeof(Article));
            cr.Add(Expression.Between("PostTime", begin, end));
            cr.AddOrder(Order.Desc("Id"));
            return cr.List<Article>();
            //throw new NotImplementedException();
        }
    }
}
